package com.moxiao.yfjh.module.blind.controller.admin.memberMessage;

import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.excel.core.util.ExcelUtils;
import com.moxiao.yfjh.framework.operatelog.core.annotations.OperateLog;
import com.moxiao.yfjh.module.blind.controller.admin.memberMessage.vo.*;
import com.moxiao.yfjh.module.blind.convert.memberMessage.MemberMessageConvert;
import com.moxiao.yfjh.module.blind.dal.dataobject.memberMessage.MemberMessageDO;
import com.moxiao.yfjh.module.blind.service.member.BlindMemberUserService;
import com.moxiao.yfjh.module.blind.service.memberMessage.MemberMessageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 相亲聊天记录")
@RestController
@RequestMapping("/blind/member-message")
@Validated
public class MemberMessageController {

    @Resource
    private MemberMessageService memberMessageService;

    @Autowired
    private BlindMemberUserService memberUserService;

    @PostMapping("/create")
    @Operation(summary = "创建相亲聊天记录")
    @PreAuthorize("@ss.hasPermission('blind:member-message:create')")
    public CommonResult<Integer> createMemberMessage(MemberMessageCreateReqVO createReqVO) {
        return success(memberMessageService.createMemberMessage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新相亲聊天记录")
    @PreAuthorize("@ss.hasPermission('blind:member-message:update')")
    public CommonResult<Boolean> updateMemberMessage(@Valid @RequestBody MemberMessageUpdateReqVO updateReqVO) {
        memberMessageService.updateMemberMessage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除相亲聊天记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blind:member-message:delete')")
    public CommonResult<Boolean> deleteMemberMessage(@RequestParam("id") Integer id) {
        memberMessageService.deleteMemberMessage(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得相亲聊天记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blind:member-message:query')")
    public CommonResult<MemberMessageRespVO> getMemberMessage(@RequestParam("id") Integer id) {
        MemberMessageDO memberMessage = memberMessageService.getMemberMessage(id);
        return success(MemberMessageConvert.INSTANCE.convert(memberMessage));
    }

    @GetMapping("/list")
    @Operation(summary = "获得相亲聊天记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('blind:member-message:query')")
    public CommonResult<List<MemberMessageRespVO>> getMemberMessageList(@RequestParam("ids") Collection<Integer> ids) {
        List<MemberMessageDO> list = memberMessageService.getMemberMessageList(ids);
        return success(MemberMessageConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得相亲聊天记录分页")
    @PreAuthorize("@ss.hasPermission('blind:member-message:query')")
    public CommonResult<PageResult<MemberMessageRespVO>> getMemberMessagePage(@Valid MemberMessagePageReqVO pageVO) {
        PageResult<MemberMessageDO> pageResult = memberMessageService.getMemberMessagePage(pageVO);
        return success(MemberMessageConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出相亲聊天记录 Excel")
    @PreAuthorize("@ss.hasPermission('blind:member-message:export')")
    @OperateLog(type = EXPORT)
    public void exportMemberMessageExcel(@Valid MemberMessageExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<MemberMessageDO> list = memberMessageService.getMemberMessageList(exportReqVO);
        // 导出 Excel
        List<MemberMessageExcelVO> datas = MemberMessageConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "相亲聊天记录.xls", "数据", MemberMessageExcelVO.class, datas);
    }


}
